﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Resources;
using System.Linq;

namespace WpfHardCodedStringsFinder
{
	/// <summary>
	/// Interaction logic for SelectProjectWindow.xaml
	/// </summary>
	public partial class SelectProjectWindow : Window
	{
		internal Common.Entities.DetailedProjectInfo SelectedProject
		{
			get;
			set;
		}

		private List<Common.Entities.DetailedProjectInfo> m_detailedInfo = null;
		private string m_rootPath = string.Empty;

		public SelectProjectWindow()
		{
			InitializeComponent();
			m_detailedInfo = new List<Common.Entities.DetailedProjectInfo>();
		}

		private void Window_Loaded(object sender, RoutedEventArgs e)
		{

			//var xmlDoc = new System.Xml.XmlDocument();
			//System.Xml.XmlNamespaceManager xnmgr = new System.Xml.XmlNamespaceManager(new System.Xml.NameTable());
			//xnmgr.AddNamespace("prj", "http://schemas.microsoft.com/developer/msbuild/2003");
			//string projectFilePath = @"E:\MyProjects\WpfTestDataManager\WpfTestDataManager\WpfTestDataManager.csproj";
			//xmlDoc.Load(projectFilePath);
			//if (xnmgr.HasNamespace("prj"))
			//{
			//    var data = xnmgr.LookupNamespace("prj");
			//}

			//LocalizableLibarary.Wrapper wr = new LocalizableLibarary.Wrapper();
			//string retData = wr.GetString("btnSearch");

			//ResourceManager rm = new ResourceManager("WpfHardCodedStringsFinder.Properties.Resources", System.Reflection.Assembly.GetExecutingAssembly());
			//string ret = rm.GetString("searchValue");

			if (IsInitialized)
			{
				LoadData();
			}
		}

		private void LoadData()
		{
			if (lstCompFilesView.ItemsSource != null && (lstCompFilesView.ItemsSource as List<Common.Entities.ProjectFileInfo>).Count > 0)
				lstCompFilesView.ItemsSource = null;

			if (treeProj.HasItems)
				treeProj.Items.Clear();

			System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
			fbd.SelectedPath = System.IO.Path.Combine(System.Environment.GetLogicalDrives()[2], "MyProjects");
			if (!string.IsNullOrEmpty(m_rootPath) && System.IO.Directory.Exists(m_rootPath))
				fbd.SelectedPath = m_rootPath;
			fbd.Description = "Select solution root folder";
			if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				m_rootPath = fbd.SelectedPath;
				TreeViewItem treeItem = new TreeViewItem();
				treeItem.Tag = "solution";
				treeItem.Header = Common.VisualStudioHelper.GetSolutionName(m_rootPath);
				treeProj.Items.Add(treeItem);

				var solution = Common.VisualStudioHelper.GetProjectsList(fbd.SelectedPath);

				if (solution == null)
					return;
				foreach (var item in solution)
				{
					TreeViewItem projIt = new TreeViewItem();
					projIt.Header = item.ProjectName;
					projIt.Tag = "project";
					projIt.DataContext = item;
					treeItem.Items.Add(projIt);
				}
			}
		}

		private void treeProj_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
		{
			var selTreeItem = (treeProj.SelectedItem as TreeViewItem);
			if (selTreeItem != null && (selTreeItem.Tag as string) == "project")
			{
				var detProjInfo = Common.VisualStudioHelper.GetDeatiledProjectInfos(System.IO.Path.Combine(m_rootPath,
									(selTreeItem.DataContext as Common.Entities.ProjectInfo).ProjectSubPath));

				if (detProjInfo != null)
				{
					SelectedProject = detProjInfo;
					lstCompFilesView.ItemsSource = detProjInfo.CompiledFiles;
				}
			}
		}

		private void miReloadData_Click(object sender, RoutedEventArgs e)
		{
			LoadData();
		}

		private void miDeselectAll_Click(object sender, RoutedEventArgs e)
		{
			if (lstCompFilesView.ItemsSource != null && (lstCompFilesView.ItemsSource as List<Common.Entities.ProjectFileInfo>).Count > 0)
			{
				var dataList = (lstCompFilesView.ItemsSource as List<Common.Entities.ProjectFileInfo>);
				foreach (Common.Entities.ProjectFileInfo file in dataList)
				{
					if (file.IsSelected)
						file.IsSelected = false;
				}
			}
		}

		private void miProcessFiles_Click(object sender, RoutedEventArgs e)
		{
			if (lstCompFilesView.ItemsSource != null && (lstCompFilesView.ItemsSource as List<Common.Entities.ProjectFileInfo>).Count > 0)
			{
				string fname = string.Empty;

				foreach (Common.Entities.ProjectFileInfo file in (lstCompFilesView.ItemsSource as List<Common.Entities.ProjectFileInfo>))
				{
					if (file.IsSelected)
					{
						var selTreeItem = (treeProj.SelectedItem as TreeViewItem);
						fname = System.IO.Path.Combine(System.IO.Path.GetDirectoryName((selTreeItem.DataContext as Common.Entities.ProjectInfo).ProjectSubPath),
														file.File);
						break;
					}
				}
				if (!string.IsNullOrEmpty(fname))
				{
					MainWindow main = new MainWindow();
					main.Owner = this;
					main.FilesToSearch = System.IO.Path.Combine(this.m_rootPath, fname);
					string projectFolder = System.IO.Path.GetDirectoryName(((treeProj.SelectedItem as TreeViewItem).DataContext
																			as Common.Entities.ProjectInfo).ProjectSubPath);
					main.ProjetcRootPath = System.IO.Path.Combine(m_rootPath, projectFolder);
					main.Show();
					main.UpdateLayout();
				}
			}
		}

		private void miGetDuplStr_Click(object sender, RoutedEventArgs e)
		{
			if (!this.SelectedProject.ContainsResourceFile)
			{
				MessageBox.Show(this, "There are no resource files in project", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
				return;
			}

			var files = this.SelectedProject.ResoucesFiles;
			if (files != null)
			{
				files.ForEach(item =>
					{
						item.Name = System.IO.Path.Combine(m_rootPath,
						   ((treeProj.SelectedItem as TreeViewItem).DataContext as Common.Entities.ProjectInfo).ProjectName,
						   item.Name);
					});

				Views.ResXSelectionWindow wind = new Views.ResXSelectionWindow();
				wind.Owner = this;
				wind.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
				wind.WindowStyle = System.Windows.WindowStyle.ToolWindow;
				wind.ResourcesX = files;
				if (wind.ShowDialog() == true)
				{
					Common.DuplicateStringFinder mfinder = new Common.DuplicateStringFinder(wind.SelectedResource.Name,
						SelectedProject);

					IEnumerable<Common.Entities.DuplicateStringInfo> infos = mfinder.Process();

					Views.DuplicateStringsWindow dupWind = new Views.DuplicateStringsWindow();
					dupWind.RootNamespace = SelectedProject.RootNamespace;

					dupWind.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
					dupWind.WindowStyle = System.Windows.WindowStyle.ToolWindow;
					dupWind.ShowActivated = true;

					if (dupWind.DuplicateStrings == null)
						dupWind.DuplicateStrings = new List<Common.Entities.DuplicateStringInfo>();

					foreach(Common.Entities.DuplicateStringInfo item in infos)
					{
						dupWind.DuplicateStrings.Add(item);
					}

					dupWind.ShowDialog();
				}
			}
		}
	}
}
